package com.zhongyu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhongyu.mapper.OrderDetailMapper;
import com.zhongyu.mapper.OrderMapper;
import com.zhongyu.mapper.StoreDetailMapper;
import com.zhongyu.mapper.StoreOperMapper;
import com.zhongyu.pojo.*;
import com.zhongyu.service.OrderDetailService;
import com.zhongyu.vo.ResultVo;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.List;

@Service("OrderDetailService")
public class OrderDetailServiceImpl  extends ServiceImpl<OrderDetailMapper, OrderDetail> implements OrderDetailService {
    @Resource
    private OrderDetailMapper orderDetailMapper;
@Resource
    private OrderDetail orderDetail;
@Resource
private Order order;
@Resource
private OrderMapper orderMapper;
@Resource
    private StoreOper storeOper;
    @Resource
    private StoreDetailMapper storeDetailMapper;
    @Resource
    private StoreOperMapper storeOperMapper;
    @Override
    public List<OrderDetail> search() {
        return baseMapper.selectList(null);
    }

    @Override
    public List<OrderDetail> searchAll(Integer id) {
//  orderDetailMapper.selectById(id);

        return orderDetailMapper.selectId(id);
    }

    @Override
    public void add2(OrderDetail orderDetail, HttpSession session) {
        long now = System.currentTimeMillis();
        //获取4位年份数字
        SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy");
        //获取时间戳
        String time=dateFormat.format(now);
        String info=now+"";
        //获取三位随机数
        int ran=(int) ((Math.random()*9+1)*100);
        order.setCreateTime(LocalDateTime.now());
        order.setPurchaseTime(LocalDateTime.now());
        order.setWayBillCode(time+info.substring(2,info.length())+ran);
        User loginUser= (User) session.getAttribute("loginUser");
        if (loginUser!=null){
            order.setCreator(loginUser.getId());
            order.setPurchaser(loginUser.getId());
        }
        order.setType(2);
        order.setState(Order.STATE_WEI);
        order.setProviderId(orderDetail.getProviderId());
        orderMapper.insert(order);
        //添加订单明细
        if (loginUser!=null){
            orderDetail.setStockKeeper(loginUser.getId());
        }
        orderDetail.setState(OrderDetail.STATE_NOT_OUT);
        orderDetail.setEndTime(LocalDateTime.now());
        Integer orderId=orderMapper.selectId();
        orderDetail.setOrderId(orderId);
        orderDetailMapper.insert(orderDetail);
    }


    @Override
    public OrderDetail searchRu(Integer id) {
        return orderDetailMapper.selectRu(id);
    }
    @Override
    public void add(OrderDetail orderDetail, HttpSession session) {

        long now = System.currentTimeMillis();
        //获取4位年份数字
        SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy");
        //获取时间戳
        String time=dateFormat.format(now);
        String info=now+"";
        //获取三位随机数
        int ran=(int) ((Math.random()*9+1)*100);
        order.setCreateTime(LocalDateTime.now());
        order.setPurchaseTime(LocalDateTime.now());
        order.setWayBillCode(time+info.substring(2,info.length())+ran);
        User loginUser= (User) session.getAttribute("loginUser");
        if (loginUser!=null){
            order.setCreator(loginUser.getId());
            order.setPurchaser(loginUser.getId());
        }
        order.setType(1);
        order.setState(Order.STATE_CREATE);
        order.setProviderId(orderDetail.getProviderId());
        orderMapper.insert(order);
        //添加订单明细
        if (loginUser!=null){
            orderDetail.setStockKeeper(loginUser.getId());
        }
        orderDetail.setState(OrderDetail.STATE_NOT_IN);
        orderDetail.setEndTime(LocalDateTime.now());
        Integer orderId=orderMapper.selectId();
        orderDetail.setOrderId(orderId);
        orderDetailMapper.insert(orderDetail);
    }
    @Override
    public void add(StoreDetail storeDetail, HttpSession session) {
        User loginUser= (User) session.getAttribute("loginUser");
       /* orderDetail.setState(OrderDetail.STATE_IN);
        User loginUser= (User) session.getAttribute("loginUser");
        if (loginUser!=null){
            orderDetail.setStockKeeper(loginUser.getId());
        }
        orderDetail.setStoreId(storeDetail.getStoreId());

        orderDetail.setEndTime(LocalDateTime.now());*/
       /* UpdateWrapper<OrderDetail> updateWrapper=new UpdateWrapper<OrderDetail>();
        updateWrapper.set("state",OrderDetail.STATE_IN);
        updateWrapper.set("stock_keeper",loginUser.getId());
        updateWrapper.set("store_id",storeDetail.getStoreId());
        updateWrapper.set("end_time",LocalDateTime.now());*/
       //1、修改订单明细表
        orderDetailMapper.update(OrderDetail.STATE_IN,loginUser.getId(),storeDetail.getStoreId(),LocalDateTime.now(),storeDetail.getId());
        //通过查询是否存在库存信息
        QueryWrapper<StoreDetail> queryWrapper=new QueryWrapper<StoreDetail>();
        queryWrapper.eq("store_id",storeDetail.getStoreId())
                .eq("good_id",storeDetail.getGoodId());
      Integer count=storeDetailMapper.selectCount(queryWrapper);
        System.out.println("总记录数1====="+count);
        if(null!=count&&count>0){
            //如果存在库存，则进行数量修改，进行数量的累加
            storeDetailMapper.update(storeDetail.getStoreId(),storeDetail.getGoodId(),storeDetail.getNumber());
        }else{
            //如果不存在库存，则进行新增
            storeDetailMapper.zeng(storeDetail.getStoreId(),storeDetail.getGoodId(),storeDetail.getNumber());
        }
        //添加库存变更操作记录
       storeOper.setOperateId(loginUser.getId());
        storeOper.setGoodId(storeDetail.getGoodId());
        storeOper.setNumber(storeDetail.getNumber());
        System.out.println(LocalDateTime.now());
       storeOper.setOperateTime(LocalDateTime.now());
       storeOper.setStoreId(storeDetail.getStoreId());
       storeOper.setType(StoreOper.TYPE_IN);
       storeOperMapper.insert(storeOper);
        //检查是否订单下的所有明细都已经入库
       Integer count2=orderDetailMapper.selectMing(storeDetail.getId());
        System.out.println("记录数2===="+count2);
       if(count2==null){
           //所有明细都已经入库，则更新订单状态为已入库
         /* order.setState(Order.STATE_END);
           order.setStockKeeper(loginUser.getId());
           order.setStockTime(LocalDateTime.now());*/
           orderMapper.updatebu(Order.STATE_END,loginUser.getId(),LocalDateTime.now(),storeDetail.getOrderId());
       }
    }
    @Override
    public OrderDetail searchOut(Integer id) {
        return orderDetailMapper.selectOut(id);
    }
    @Override
    public void addOut(StoreDetail storeDetail, HttpSession session) throws ResultVo {
        User loginUser= (User) session.getAttribute("loginUser");

       //2、商品库存
        //通过查询是否存在库存信息
        QueryWrapper<StoreDetail> queryWrapper=new QueryWrapper<StoreDetail>();
        queryWrapper.eq("store_id",storeDetail.getStoreId())
                .eq("good_id",storeDetail.getGoodId());
        Integer count=storeDetailMapper.selectCount(queryWrapper);
        System.out.println("总记录数1====="+count);
        if(null!=count&&count>0){
            //如果存在库存，检查库存是否足够
          Integer nu=  storeDetailMapper.select(storeDetail.getStoreId(),storeDetail.getGoodId());
            System.out.println(nu);
         Integer num=nu-storeDetail.getNumber();
         if(num>=0){
             //库存充足，则更新库存数量
          storeDetailMapper.updateChu(storeDetail.getStoreId(),storeDetail.getGoodId(),num);
             //添加库存变更操作记录
             storeOper.setOperateId(loginUser.getId());
             storeOper.setGoodId(storeDetail.getGoodId());
             storeOper.setNumber(storeDetail.getNumber());
             System.out.println(LocalDateTime.now());
             storeOper.setOperateTime(LocalDateTime.now());
             storeOper.setStoreId(storeDetail.getStoreId());
             storeOper.setType(StoreOper.TYPE_OUT);
             storeOperMapper.insert(storeOper);
             //修改订单明细表
             orderDetailMapper.update(OrderDetail.STATE_OUT,loginUser.getId(),storeDetail.getStoreId(),LocalDateTime.now(),storeDetail.getId());
         }else{
                 //库存不足，则提示用户

                 throw ResultVo.failure("库存不足") ;


         }

        }else{
            throw ResultVo.failure("不存在改商品的库存信息") ;
        }

        //检查是否订单下的所有明细都已经出库
        Integer count2=orderDetailMapper.selectXian(storeDetail.getId());
        System.out.println("记录数2===="+count2);
        if(count2==null){
            //所有明细都已经出库，则更新订单状态为已出库
            orderMapper.updatebu(Order.STATE_CHU,loginUser.getId(),LocalDateTime.now(),storeDetail.getOrderId());
        }
    }
}
